package com.moss.cloud.base.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moss.cloud.base.api.model.from.BaseFrom;
import com.moss.cloud.base.api.model.from.FlowModelFrom;
import com.moss.cloud.base.api.model.param.FlowModelParam;
import com.moss.cloud.base.api.model.vo.FlowModelVO;
import com.moss.cloud.base.biz.mapper.FlowModelMapper;
import com.moss.cloud.base.biz.model.FlowModel;
import com.moss.cloud.base.biz.service.IFlowModelService;
import com.moss.cloud.common.base.utils.PageUtil;
import com.moss.cloud.common.core.exception.SystemErrorType;
import com.moss.cloud.common.core.utils.BooleanHandel;
import com.moss.cloud.common.core.utils.UserContextHolder;
import com.moss.cloud.common.flow.enums.ModelCompletionMethodEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 流程模型业务实现
 *
 * @author 瑾年
 * @date 2024-02-06
 */
@Slf4j
@Service
public class FlowModelServiceImpl extends ServiceImpl<FlowModelMapper, FlowModel> implements IFlowModelService {

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Override
    public IPage<FlowModelVO> page(FlowModelParam param) {
        Page<FlowModel> page = PageUtil.createPage(param.getCurrent(), param.getSize());
        return baseMapper.flowModelPage(page,UserContextHolder.getTenantId(),param);
    }
    /**
     * 流程模型列表
     *
     * @param param
     * @return
     */
    @Override
    public List<FlowModelVO> flowModelList(FlowModelParam param){
        return baseMapper.flowModelPage(UserContextHolder.getTenantId(),param);
    }

    /**
     * 新增
     *
     * @param from
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveFlowModel(FlowModelFrom from) {
        this.checkFlowModelName(from, Boolean.TRUE);
        FlowModel flowModel = new FlowModel();
        BeanUtils.copyProperties(from, flowModel);
        flowModel.setTenantId(UserContextHolder.getTenantId());
        return this.save(flowModel);
    }

    /**
     * 更新
     *
     * @param from
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(FlowModelFrom from) {
        BooleanHandel.trueThrow(Objects.isNull(from.getId())).throwMessage(SystemErrorType.PRIMARY_KEY);
        this.checkFlowModelName(from, Boolean.FALSE);
        FlowModel flowModel = this.getById(from.getId());
        this.checkData(flowModel);
        FlowModel model = new FlowModel();
        BeanUtils.copyProperties(from, model);
        return this.updateById(model);
    }

    /**
     * 验证模型名称不能重复
     *
     * @param from
     * @param isAdd
     */
    private void checkFlowModelName(FlowModelFrom from, boolean isAdd) {
        List<FlowModel> flowModels = this.list(new QueryWrapper<FlowModel>().lambda().eq(FlowModel::getFlowModelName, from.getFlowModelName()));
        BooleanHandel.trueThrow(isAdd && CollectionUtils.isNotEmpty(flowModels)).throwMessage(SystemErrorType.FLOW_NAME);
        BooleanHandel.trueThrow(ModelCompletionMethodEnum.MODEL_ADMIN.getType().equals(from.getFlowModelCompletionMethod()) &&
                Objects.isNull(from.getFlowModelAdmin())).throwMessage(SystemErrorType.MODEL_ADMIN);
        BooleanHandel.isTrue(!isAdd).trueHandle(() -> {
            flowModels.removeIf(item -> item.getId().equals(from.getId()));
            BooleanHandel.trueThrow(CollectionUtils.isNotEmpty(flowModels)).throwMessage(SystemErrorType.FLOW_NAME);
        });
    }


    /**
     * 删除
     *
     * @param baseFrom
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean remove(BaseFrom baseFrom) {
        FlowModel flowModel = this.getById(baseFrom.getId());
        this.checkData(flowModel);
        return this.removeById(baseFrom.getId());
    }

    /**
     * 验证数据所属权
     *
     * @param flowModel
     */
    private void checkData(FlowModel flowModel) {
        BooleanHandel.trueThrow(Objects.isNull(flowModel)).throwMessage(SystemErrorType.DATE_EMPTY);
        BooleanHandel.trueThrow(!flowModel.getTenantId().equals(UserContextHolder.getTenantId())).throwMessage(SystemErrorType.NOT_BELONG_TENANT);
    }

    /**
     * 详情
     *
     * @param baseFrom
     * @return
     */
    @Override
    public FlowModel detail(BaseFrom baseFrom){
        return this.getById(baseFrom.getId());
    }

}
